import { $get, $post } from '@/@api/index';
import Taro from '@tarojs/taro'
/**
 * 快捷登录
 *
 * @param {string} code   登录code
 */
interface Code {
    code: string
}

// export const getLogin = (data: Code) => {//http://172.16.47.246:8081/wx/quickLogin
//     return new Promise<void>(async (resolve, reject) => {
//         const res = await $post<{ openid: string, session_key: string, EXTMOBILE: string,NUM:string }>('/selfmachine-auth/customer/wechatMiniProgramLogin', data)
//         console.log(res, 9);
//         if (res.data.NUM == '-1') {
//             return   Taro.showToast({
//                 icon: 'error',
//                 title: '登录失败'
//             })
//         }
//         if (res.data.EXTMOBILE) {
//             if (res.data.openid && res.data.session_key) {
//                 Taro.setStorageSync('userInfo', JSON.stringify(res.data))
//                 Taro.redirectTo({
//                     url: "/pages/index/index",
//                 })
//             }
//             resolve()
//         } else {
//             Taro.showToast({
//                 icon: 'error',
//                 title: '请先绑定手机号'
//             })
//             reject('未注册')
//         }
//     })
// }

export const getLogin = (data: Code) => {//http://172.16.47.246:8081/wx/quickLogin
    return new Promise<void>(async (resolve, reject) => {
        const res = await $get<any>('/selfmachine-auth/customer/wechatMiniProgramLogin', data)
        if(res.statusCode === 200 && res.data.success){
            resolve(res.data)
        }
        else {
            Taro.showToast({
                icon: 'error',
                title: res.data.error || '微信登录失败'
                //title: res.data.error || '微信登录失败'
            })
            reject('登录失败')
        }
    })
}



/**
 *
 * 小程序注册
 * @param tel 手机号
 * @param mobile 手机号
 * @param code 登录code
 * @param openid  唯一识别码
 * @param nickname 微信昵称
 *
 */
interface LogonParams {
    tel: string
    mobile: string
    code: string
    openid: string
    nickname: string

}
export const getLogon = async (data: Code, data2: LogonParams) => {

    const res = await $post<{ openid: string, session_key: string, EXTMOBILE: string }>('http://172.16.47.246:8081/wx/quickLogin', data)
    console.log(res, 57);
    if (res.data.openid && res.data.session_key) {
        data2.openid = res.data.openid
        const res2 = await $post<{ code: number, msg: string }>('http://172.16.47.246:8081/wx/phoneLogin', data2)
        console.log(res2, 59);
        if (res2.data.code == 0) {
            Taro.setStorageSync('userInfo', JSON.stringify(res.data))
            Taro.showToast({
                icon: 'success',
                title: '注册成功'
            })
            setTimeout(() => {
                Taro.reLaunch({
                    url: "/pages/index/index",
                })
            }, 1000);
        } else {
            Taro.showToast({
                icon: 'error',
                title: res2.data.msg,

            })
        }
    }

}

/**
 *
 *获取短信验证码
 *
 * @param  mobile  注册手机号
 *
 *
 *
 *
 */
interface VerificationCode {

    phone: string

}
export const getVerificationCode = async (data: VerificationCode) => {
    const res = await $get<any>('/selfmachine-auth/customer/verify', data)
    if (res.data.code == 200) {
        Taro.showToast({
            icon: 'success',
            title: '发送成功'
        })
    } else {
        Taro.showToast({
            icon: 'error',
            title: '发送失败'
        })
    }

}

interface VerificationCode {
    phone: string
    verify: string
}

export const doLoginByPhone = (data: any) => {

    return new Promise<void>(async (resolve, reject) => {
        const res = await $post<any>('/selfmachine-auth/customer/phoneLogin', data)
        if(res.statusCode === 200){
            resolve(res.data)
        }
        else {
            Taro.showToast({
                icon: 'error',
                title: res.data.error || '手机号登录失败'
                //title: res.data.error || '微信登录失败'
            })
            reject('登录失败')
        }
    })


}

